package jp.sourceforge.akjrcp.ui.param;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseListener;
import org.eclipse.swt.events.MouseMoveListener;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Scale;

public class ParamScale extends AbstractParamCompositeItem{

private String key;
private String label;


private Label labelItem;

private String value="";
private Scale scale;

private Label footLabelItem;

private int offset;	//for get minus value;
public String getKey() {
	return key;
}

public String getLabel() {
	return label;
}


public void setToolTipText(String text){
	labelItem.setToolTipText(text);
	this.scale.setToolTipText(text);
}


/* the vaues is double
 * (non-Javadoc)
 * @see jp.sourceforge.akjrcp.ui.param.ParamItem#getValue()
 */
public String getValue() {
	return ""+(scale.getSelection()+offset);
}


public void setValue(int value){
	this.value=""+value;
	scale.setSelection(value-offset);
	updateLabel();
}
public void setValue(String value) {
	this.value=value;
	if(value.length()>0){//set valid value
		scale.setSelection(Integer.parseInt(value)-offset);
		updateLabel();
	}
}

public ParamScale(Composite parent, int style,String key,String label,int min,int current,int max) {
	this(parent,style,key,label);
	setMinimum(min);
	setValue(current);
	setMaximum(max);
}
	public ParamScale(Composite parent, int style,String key,String label) {
		super(parent, style);
		
		this.key=key;
		this.label=label;
		
		this.setLayout(new GridLayout(2,false));
		labelItem = new Label(this,SWT.NONE);
		//labelItem.setText(label);
		
		scale=new Scale(this,SWT.NULL);
		
		scale.addSelectionListener(new LabelUpdate());
		Listener dragListener = new Listener() {
			public void handleEvent(Event event) {
				 switch ( event.type ) {
				  case SWT.MouseWheel:
					  updateLabel();
					  break;  
				 	}
				}
		};
		scale.addListener(SWT.MouseWheel, dragListener);
		
		
		//modify check
		scale.addSelectionListener(new SelectionCheck());
		scale.addListener(SWT.MouseWheel, new WheelCheck());
		
		updateLabel();
	}
	

	
	public class LabelUpdate implements SelectionListener{

		public void widgetDefaultSelected(SelectionEvent e) {
			updateLabel();
		}

		public void widgetSelected(SelectionEvent e) {
			updateLabel();
		}
		
	}
	Pattern pattern=Pattern.compile("%(\\d+)d");
	
	protected String toValue(String n){
		int num=Integer.parseInt(n);
		String ret=getValue();
		while(ret.length()<num){
			ret="0"+ret;	//make a problem when offset is minus
			}
		return ret;
	}
	public void updateLabel(){
	
		int p=label.indexOf("%");
		if(p!=-1){
			Matcher matcher=pattern.matcher(label);
			StringBuffer sb = new StringBuffer();
			while (matcher.find()) {
			  String num=matcher.group(1);
			  String value=toValue(num);
			  matcher.appendReplacement(sb, value);
			}
			matcher.appendTail(sb);
			labelItem.setText(sb.toString());
			return;
		}
		//simple
		labelItem.setText(label);
	}
	
	public boolean isModified() {
		
		String oldValue=getValue();
		
		
		
		return !oldValue.equals(value);
	}

	public void setMaximum(int max){
		scale.setMaximum(max);
	}
	public void setMinimum(int min){
		scale.setMinimum(min);
	}

	public Scale getScale() {
		return scale;
	}

	public void setScale(Scale scale) {
		this.scale = scale;
	}

	public int getOffset() {
		return offset;
	}

	public void setOffset(int offset) {
		this.offset = offset;
		updateLabel();
	}






}
